/*
-----------------------------------------------------------------------------
This source file is part of Labor3D
(Labor 3D Graphics Engine)

Copyright (c) 2009-2020 Rock Mao
* creator : Rock Mao
* created : 2011-2-11   11:37
* filename: LaborBaseMesh.cpp
-----------------------------------------------------------------------------
*/
#include "stdafx.h"

#include "LaborBaseMesh.h"
#include "../LaborCore.h"
#include "../Renderer/LaborRenderSystemHeader.h"
NAMESPACE_LABOR3D_BEGIN
IMPL_BASE_OBJECT_CLASSID(CLBaseMesh  , IRenderSysResource);

//---------------------------------------------------------------------
CLBaseMesh::CLBaseMesh(LaborBaseTextureMgr * pTextureMgr)
{
	m_pTexMgr    = pTextureMgr;
	m_pRenderSys = m_pTexMgr->renderSys();

}

//---------------------------------------------------------------------
CLBaseMesh::~CLBaseMesh()
{

}

bool CLBaseMesh::load(const wchar_t* fileName , unsigned long  arg)
{
	return true;
}

bool CLBaseMesh::load(const wchar_t* fileName , const xint8_u* buf , size_t bufLen, unsigned long arg)
{
	return true;
}

bool CLBaseMesh::isLoaded()
{
	return m_nVertex > 0;
}

bool CLBaseMesh::unload()
{
	m_nFace = 0 ;
	m_nVertex = 0 ;
	m_name = L"";
	m_nSubMesh = 0;

	return true;
}

unsigned long CLBaseMesh::memUsage()
{
	return 1;
}

bool CLBaseMesh::render()
{
	UINT stride = m_nVertexSize;
	UINT offset = 0;
	m_pRenderSys->setVertexBuffer( 0, 1, m_pVertexBuffer, &stride, &offset );
	m_pRenderSys->setIndexBuffer( m_IndexBuffers[0], IT_32BIT );
	m_pRenderSys->setPrimitiveTopology( eL_PT_TRIANGLELIST );

	m_pRenderSys->drawIndexed( m_nFace*3, 0, 0 );
	//m_pRenderSys->drawIndexed( 3, 27, 0 );

	return true;
}

////////////////////////////////////////////////////////////////////////////
//void XRESOURCE_ReleaseObject(CLBaseMesh* ptr)
//{
//	if(ptr) ptr->ReleaseObject();
//	return ;        
//}
//
//void XRESOURCE_AddRef(CLBaseMesh* ptr)
//{
//	if(ptr) ptr->AddRef();
//	return ;
//}
ILInputLayout* CLBaseMesh::createInputLayout(int nUVChanel , ILRenderSystem* pRenderSys , bool bSkin)
{
	wchar_t _name[32] = {0};
	if(bSkin)
	{
		return NULL;
	}
	else
	{
		swprintf( _name, L"StaticMeshIA_%0dUV" , nUVChanel );
		ILInputLayout* pIL = pRenderSys->getInputLayout(_name);
		if(pIL != NULL) 
			return pIL;

		CLInputLayoutDesc InputDesc;
		int BuffIdx = 0;
		InputDesc.addElement(SHADER_SEMANTIC_POSITION     , eL_SVT_FLOAT4 , 0 , BuffIdx);
		InputDesc.addElement(SHADER_SEMANTIC_NORMAL       , eL_SVT_FLOAT4 , 0 , BuffIdx);
		InputDesc.addElement(SHADER_SEMANTIC_COLOR          , eL_SVT_FLOAT4 , 0 , BuffIdx);
		InputDesc.addElement(SHADER_SEMANTIC_TANGENT      , eL_SVT_FLOAT4 , 0 , BuffIdx);
		for(int j = 0 ; j < nUVChanel ; j ++)
		{
			InputDesc.addElement(SHADER_SEMANTIC_TEXCOORD , eL_SVT_FLOAT2 , j , BuffIdx);
		}
		//InputDesc.addBufferDesc(RESOURCE_USAGE_DEFAULT , RESOURCE_ACCESS_NONE);
		return pRenderSys->createInputLayout(_name, InputDesc);
	}
}

NAMESPACE_LABOR3D_END
